﻿Imports Entities
Public Class VillaDAL
    Public Shared Function List() As List(Of VILLAS)

        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.VILLAS
                            Where rec.ACTIVO

                Return query.ToList

            End Using
        Catch ex As Exception
            Return Nothing
        End Try

    End Function
    Public Shared Function Create(ByVal zObject As VILLAS) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                bd.Villas.Add(zObject)
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Debug.Print("VillaDAL.Create() " + ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Update(ByVal zObject As VILLAS) As VILLAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.VILLAS
                              Where rec.VILLA = zObject.VILLA
                              Select rec).SingleOrDefault

                query.NOMBRE = zObject.NOMBRE
                query.ACTIVO = True
                bd.SaveChanges()
                Return query
            End Using
        Catch ex As Exception
            Debug.Print("VillaDAL.Update() " + ex.Message)
            Return Nothing
        End Try
    End Function
    Public Shared Function Delete(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim del = (From rec In bd.VILLAS
                          Where rec.VILLA = zId And rec.ACTIVO
                          Select rec).Single

                del.ACTIVO = False
                bd.SaveChanges()
                Return True
            End Using
        Catch ex As Exception
            Debug.Print("VillaDAL.Delete() " + ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function Exist(ByVal zId As Long) As Boolean
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = (From rec In bd.VILLAS
                            Where rec.VILLA = zId
                            Select rec).Count
                If query > 0 Then
                    Return True
                Else
                    Return False
                End If
            End Using
        Catch ex As Exception
            Debug.Print("VillaDAL.Exist() " + ex.Message)
            Return False
        End Try
    End Function
    Public Shared Function SearchById(ByVal zId As Long) As VILLAS
        Try
            Using bd As New gas_gasdesuvecinoEntities
                Dim query = From rec In bd.VILLAS
                           Where rec.VILLA = zId And rec.ACTIVO

                Return query.SingleOrDefault
            End Using
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
End Class
